package com.ruoyi.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * MyBatisPlus自动填充处理器
 */
@Component
public class MyBatisPlusAutoFillHandler implements MetaObjectHandler {

    private final Logger log = LoggerFactory.getLogger(MyBatisPlusAutoFillHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入填充...");
        this.strictInsertFill(metaObject, "createId", Long.class, SecurityUtils.getUserId());
        this.strictInsertFill(metaObject, "createName", String.class, SecurityUtils.getUsername());
        this.strictInsertFill(metaObject, "createTime", Date.class, DateUtils.getNowDate());
        this.strictInsertFill(metaObject, "updateId", Long.class, SecurityUtils.getUserId());
        this.strictInsertFill(metaObject, "updateName", String.class, SecurityUtils.getUsername());
        this.strictInsertFill(metaObject, "updateTime", Date.class, DateUtils.getNowDate());
        this.strictInsertFill(metaObject, "delFlag", String.class, "0");
        this.strictInsertFill(metaObject, "status", String.class, "0");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充...");
        this.strictUpdateFill(metaObject, "updateId", Long.class, SecurityUtils.getUserId());
        this.strictUpdateFill(metaObject, "updateName", String.class, SecurityUtils.getUsername());
        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateUtils.getNowDate());
    }

}
